<html>
<head><meta charset="utf-8"><title>type inference unsuccessful with MaybeUninit · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html">type inference unsuccessful with MaybeUninit</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206412551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412551">(Aug 09 2020 at 21:32)</a>:</h4>
<p>I just ran into an interesting issue with type inference.</p>



<a name="206412555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412555">(Aug 09 2020 at 21:32)</a>:</h4>
<p>I wrote this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">waitid_pidfd</span><span class="p">(</span><span class="n">pidfd</span>: <span class="nc">RawFd</span><span class="p">,</span><span class="w"> </span><span class="n">wnohang</span>: <span class="kt">bool</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">io</span>::<span class="nb">Result</span><span class="o">&lt;</span><span class="p">(</span><span class="n">libc</span>::<span class="n">siginfo_t</span><span class="p">,</span><span class="w"> </span><span class="n">libc</span>::<span class="n">rusage</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">siginfo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MaybeUninit</span>::<span class="n">uninit</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">rusage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MaybeUninit</span>::<span class="n">uninit</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">flags</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">wnohang</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">libc</span>::<span class="n">WNOHANG</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">syscall_result</span><span class="p">(</span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">libc</span>::<span class="n">syscall</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="n">libc</span>::<span class="n">SYS_waitid</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">P_PIDFD</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">pidfd</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">siginfo</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">(),</span><span class="w"></span>
<span class="w">            </span><span class="n">flags</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">rusage</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">siginfo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">siginfo</span><span class="p">.</span><span class="n">assume_init</span><span class="p">()</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">wnohang</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">ret</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">siginfo</span><span class="p">.</span><span class="n">si_pid</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">io</span>::<span class="n">ErrorKind</span>::<span class="n">WouldBlock</span><span class="p">.</span><span class="n">into</span><span class="p">())</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">((</span><span class="n">siginfo</span><span class="p">,</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">rusage</span><span class="p">.</span><span class="n">assume_init</span><span class="p">()</span><span class="w"> </span><span class="p">}))</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="206412564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412564">(Aug 09 2020 at 21:33)</a>:</h4>
<p>And I got this error:</p>
<div class="codehilite"><pre><span></span><code><span class="n">error</span><span class="p">[</span><span class="n">E0282</span><span class="p">]</span>: <span class="nc">type</span><span class="w"> </span><span class="n">annotations</span><span class="w"> </span><span class="n">needed</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="err">`</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="err">`</span><span class="w"></span>
<span class="w">  </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">lib</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">41</span>:<span class="mi">31</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w"></span>
<span class="mi">27</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="kd">let</span><span class="w"> </span><span class="n">siginfo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MaybeUninit</span>::<span class="n">uninit</span><span class="p">();</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w">         </span><span class="o">-------</span><span class="w"> </span><span class="n">consider</span><span class="w"> </span><span class="n">giving</span><span class="w"> </span><span class="err">`</span><span class="n">siginfo</span><span class="err">`</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">explicit</span><span class="w"> </span><span class="k">type</span> <span class="err">`</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">with</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="k">type</span> <span class="nc">parameters</span><span class="w"> </span><span class="n">specified</span><span class="w"></span>
<span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="mi">41</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="k">if</span><span class="w"> </span><span class="n">wnohang</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">ret</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">siginfo</span><span class="p">.</span><span class="n">si_pid</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="w">                               </span><span class="o">^^^^^^^</span><span class="w"> </span><span class="n">cannot</span><span class="w"> </span><span class="n">infer</span><span class="w"> </span><span class="k">type</span>
   <span class="o">|</span><span class="w"></span>
<span class="w">   </span><span class="o">=</span><span class="w"> </span><span class="n">note</span>: <span class="nc">type</span><span class="w"> </span><span class="n">must</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">known</span><span class="w"> </span><span class="n">at</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">point</span><span class="w"></span>
</code></pre></div>



<a name="206412565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412565">(Aug 09 2020 at 21:33)</a>:</h4>
<p>In theory, shouldn't Rust be able to infer the type?</p>



<a name="206412576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412576">(Aug 09 2020 at 21:33)</a>:</h4>
<p>It knows the <code>siginfo</code> in the return value is a <code>libc::siginfo_t</code>.</p>



<a name="206412617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412617">(Aug 09 2020 at 21:34)</a>:</h4>
<p>Which means the previous <code>siginfo.assume_init()</code> must return <code>libc::siginfo_t</code>, so the previous <code>siginfo</code> has type <code>MaybeUninit&lt;libc::siginfo_t&gt;</code>.</p>



<a name="206412619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412619">(Aug 09 2020 at 21:34)</a>:</h4>
<p>What prevents Rust from inferring that?</p>



<a name="206412620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412620">(Aug 09 2020 at 21:34)</a>:</h4>
<p>field accesses and inherent method calls will stall type inference</p>



<a name="206412630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412630">(Aug 09 2020 at 21:35)</a>:</h4>
<p>It's a limitation of the current type inference engine</p>



<a name="206412633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412633">(Aug 09 2020 at 21:35)</a>:</h4>
<p>Ah. So, it isn't that Rust <em>couldn't</em>, it's that it assumes at the point of <code>siginfo.si_pid</code> that if it doesn't know at that point something must have gone wrong?</p>



<a name="206412679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412679">(Aug 09 2020 at 21:36)</a>:</h4>
<p>Not so much "gone wrong", but it has no concept of deferring type-checking a field access, so if the type is not already inferred at that point it will fail</p>



<a name="206412695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412695">(Aug 09 2020 at 21:37)</a>:</h4>
<p>Got it.</p>



<a name="206412696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412696">(Aug 09 2020 at 21:37)</a>:</h4>
<p>Easy enough to fill in a type signature there.</p>



<a name="206412800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412800">(Aug 09 2020 at 21:41)</a>:</h4>
<p>...now to figure out why there's no <code>si_pid</code> field...</p>



<a name="206412804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412804">(Aug 09 2020 at 21:41)</a>:</h4>
<p>Ah. Sigh.</p>



<a name="206412990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206412990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206412990">(Aug 09 2020 at 21:47)</a>:</h4>
<p><a href="https://github.com/rust-lang/libc/issues/1381">https://github.com/rust-lang/libc/issues/1381</a></p>



<a name="206413898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/type%20inference%20unsuccessful%20with%20MaybeUninit/near/206413898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/type.20inference.20unsuccessful.20with.20MaybeUninit.html#206413898">(Aug 09 2020 at 22:14)</a>:</h4>
<p><a href="https://github.com/rust-lang/libc/pull/1858">https://github.com/rust-lang/libc/pull/1858</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>